home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7444 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.4 KB  |  156 lines

  1. Path: rational.com!usenet
  2. From: Jerome Desquilbet <jDesquilbet@Rational.COM>
  3. Newsgroups: comp.lang.ada,comp.lang.c++
  4. Subject: Re: on OO differnces between Ada95 and C++
  5. Date: Fri, 23 Feb 1996 10:55:03 +0100
  6. Organization: Rational Software Corporation, France
  7. Message-ID: <312D8EF7.167E@Rational.COM>
  8. References: <4gbq7q$g08@qualcomm.com> <3129F185.41C6@Rational.COM> <4gi413$qo1@druid.borland.com>
  9. NNTP-Posting-Host: pigalle.rational.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; OSF1 V3.2 alpha)
  14.  
  15. ...YET ANOTHER RELIGIOUS WAR...
  16.  
  17. Pete Becker wrote:
  18.  
  19. > >#define private public // *** BERK! ***
  20. > >#include "...h"        // second definition for the same class
  21. > >#undef private
  22. > This is not true. A program that attempts to do this violates the one
  23. > definition rule, so it is not a legal C++ program.
  24.  
  25. Pete,
  26.  
  27. What is this "one definition rule"? Could you point me to the
  28. appropriate C++ Draft Standard location?
  29.  
  30. Try this:
  31.  
  32. // shape.h -----------------------------------------------------------
  33.  
  34. #ifndef SHAPE_H_
  35. #define SHAPE_H_
  36. class Shape {
  37.   public:
  38.     Shape();
  39.     virtual void Draw  ()                         const = 0;
  40.     virtual void Erase ()                         const = 0;
  41.             void Move  (int Delta_X, int Delta_Y);
  42.             void Rotate(int Delta_A);
  43.   private:
  44.     int X, Y, A;
  45. };
  46. #endif
  47.  
  48. // shape.C -----------------------------------------------------------
  49.  
  50. #include "shape.h"
  51. #include <iostream.h>
  52.  
  53. Shape::Shape() {X=Y=A=0;}
  54.  
  55. void Shape::Move(int Delta_X, int Delta_Y) {
  56.   cout << "I am a shape and I begin to move from ("
  57.        << X << ',' << Y << ").\n";
  58.   Erase(); X+=Delta_X; Y+=Delta_Y; Draw();
  59.   cout << "I am a shape and I have finished moving to ("
  60.        << X <<',' << Y << ").\n";
  61. }
  62. void Shape::Rotate(int Delta_A) {
  63.   cout << "I am a shape and I begin to rotate from " << A << ".\n";
  64.   Erase(); A+=Delta_A; Draw();
  65.   cout << "I am a shape and I have finished rotating to " << A << ".\n";
  66. }
  67.  
  68. // rectangle.h --------------------------------------------------------
  69.  
  70. #ifndef RECTANGLE_H_
  71. #define RECTANGLE_H_
  72. #include "shape.h"
  73. class Rectangle : public Shape {
  74.   public:
  75.     Rectangle();
  76.     void Draw  ()                         const;
  77.     void Erase ()                         const;
  78.     void Resize(int Delta_H, int Delta_W);
  79.   private:
  80.     int H, W;
  81. };
  82. #endif
  83.  
  84. // rectangle.C --------------------------------------------------------
  85.  
  86. #include "rectangle.h"
  87. #include <iostream.h>
  88.  
  89. Rectangle::Rectangle() : Shape() {H=W=0;}
  90.  
  91. void Rectangle::Draw() const {
  92.   cout << "I am a rectangle and I draw myself.\n";
  93. }
  94. void Rectangle::Erase() const {
  95.   cout << "I am a rectangle and I erase myself.\n";
  96. }
  97. void Rectangle::Resize(int Delta_H, int Delta_W) {
  98.   cout << "I am a rectangle and I begin to change my size.\n";
  99.   Erase(); H+=Delta_H; W+=Delta_W; Draw();
  100.   cout << "I am a rectangle and I have finished changing my size.\n";
  101. }
  102.  
  103. // try_shape_rectangle_dirty_way.C ------------------------------------
  104.  
  105. #define private public // *** BERK! ***
  106. #include "rectangle.h" // second definition for class Rectangle
  107. #undef private        // in the program
  108. int main() {
  109.   Rectangle R;
  110.   Shape*    pS;
  111.   pS = &R;
  112.   pS->X = 8;
  113.   pS->Y = 18;
  114.   pS->Move(10, 20);
  115. }
  116.  
  117.  
  118. It gives the following log:
  119.  
  120.  
  121. I am a shape and I begin to move from (8,18).
  122. I am a rectangle and I erase myself.
  123. I am a rectangle and I draw myself.
  124. I am a shape and I have finished moving to (18,38).
  125.  
  126.  
  127. Note that the initial coordinates were (8,18).
  128.  
  129. The whole program contains two definitions for the same classes Shape
  130. and Rectangle: for each, one definition with attributes private and
  131. another definition with everything public.
  132.  
  133. It seems that in C++, every encapsulation can be legally broken,
  134. essentially because of _independent_ compilation that allows
  135. redefinitions of the same class.
  136.  
  137. Ada compilation model is different: it's _separate_ compilation.
  138. Impossible in Ada to have a redefinition in the same program: it's
  139. detected at compile-time. If something is checked in C++, it's deferred
  140. to link-time (for example a redefinition of the same class that will
  141. clash because the two constructors will have the same name).
  142.  
  143.  
  144. Read also "The design and evolution of C++" by Bjarne Stroustrup,
  145. chapter 17 "Namespaces", section 3 "Ideals for a solution": Ada has a
  146. more complete solution than C++.
  147.  
  148. The basic property of OO languages is encapsulation. According to this
  149. criteria, C++ fails.
  150.  
  151. Non mais enfin, quoi !
  152.  
  153.  
  154.   Jerome.
  155.